require "common"

--3d包点注数数组
local D3BetNumPoint10 = { 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75,
						75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1 };

local D3BetNumPoint11 = {  1, 2, 1, 3, 3, 3, 4, 5, 4, 5, 5, 4, 5, 5, 4, 5,
						5, 4, 5, 4, 3, 3, 3, 1, 2, 1, 0 };

local D3BetNumPoint12 = { 0, 0, 1, 1, 2, 3, 4, 5, 7, 8, 9, 10, 10, 10, 10,
						9, 8, 7, 5, 4, 3, 2, 1, 1, 0, 0, 0 };

local D3BetNumPoint13 = { 1, 1, 2, 3, 4, 5, 7, 8, 10, 12, 13, 14, 15, 15, 15,
						15, 14, 13, 12, 10, 8, 7, 5, 4, 3, 2, 1, 1 };

--3d复式数组
local D3BetDuplex0 = { 0, 8, 27, 64, 125, 216, 343, 512, 729, 1000 };
local D3BetDuplex1 = { 0, 2, 6, 12, 20, 30, 42, 56, 72, 90 };
local D3BetDuplex2 = { 0, 0, 0, 4, 10, 20, 35, 56, 84, 120 };
local D3BetDuplex3 = { 0, 4, 10, 20, 35, 56, 84, 120, 165, 220 };
local D3BetDuplex4 = { 0, 0, 6, 24, 60, 120, 210, 336, 504, 720 };
local D3BetDuplex5 = { 0, 6, 18, 36, 60, 90, 126, 168, 216, 270 };




--随机函数,返回格式化后的字符串，c=随机号码位数，s=随机号码下限，e=随机号码上限
function play_random ( c, s, e )
	local ret = ""
	math.randomseed(microtime()*1000000)
	for i=1,c do
		ret = ret..format_num( math.random(s,e))
	end
	return ret
end

--格式化3d字符串
function format_3d( num, c )
	num = string.gsub( num, "*", "")
	local out = ""
	local len = string.len( num )
	for i=1,len do
		out = out..format_num(string.sub(num,i,i))
	end
	return out
end

--获取3d指令串包号位置
function get_baohao_pos( inStr )
	local pos=0
	if string.sub(inStr,1,1) == "*" and string.sub(inStr,2,2) ~= "*" and string.sub(inStr,3,3) ~= "*" then
		pos = 1
	elseif string.sub(inStr,1,1) ~= "*" and string.sub(inStr,2,2) == "*" and string.sub(inStr,3,3) ~= "*" then
		pos = 2
	elseif string.sub(inStr,1,1) ~= "*" and string.sub(inStr,2,2) ~= "*" and string.sub(inStr,3,3) == "*" then
		pos = 3
	elseif string.sub(inStr,1,1) ~= "*" and string.sub(inStr,2,2) == "*" and string.sub(inStr,3,3) == "*" then
		pos = 4
	elseif string.sub(inStr,1,1) == "*" and string.sub(inStr,2,2) ~= "*" and string.sub(inStr,3,3) == "*" then
		pos = 5
	elseif string.sub(inStr,1,1) == "*" and string.sub(inStr,2,2) == "*" and string.sub(inStr,3,3) ~= "*" then
		pos = 6
	else
		pos = 0
	end

	return pos
end

--数字字符串排序
function sort_str( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len do
		table.insert( st, format_num(string.sub(num,i,i)) )
	end
	table.sort(st)
	for i=1,len do
		out = out..st[i]
	end
	return out
end

function in_table ( e, t )
	for _,v in pairs(t) do
		if (v==e) then return true end
	end
	return false
end
--判断奇偶
function justjiou(str)
	local st = {}
	local out = ""
	for i = 1 ,string.len(str) do
		table.insert(st,string.sub(str,i,i) % 2)
	end
	table.sort(st)
	for i=1,table.getn(st) do
		out = out..st[i]
	end
	return out
end

--判断大小
function justdaxiao(str)
	local st = {}
	local out = ""
	for i = 1 ,string.len(str) do
		if tonumber(string.sub(str,i,i)) >= 5 then
			table.insert(st,1)
		else
			table.insert(st,0)
		end
	end
	table.sort(st)
	for i=1,table.getn(st) do
		out = out..st[i]
	end
	return out

end
--数字字符串排序
function sort_str_dj( num )
	local out = ""
	local st = {}
	local len = 0
	len = string.len(num)
	for i=1,len do
		c = string.sub(num,i,i)
		table.insert( st, c )
	end
	table.sort(st)
	for i=1,table.getn( st ) do
		out = out..st[i]
	end
	return out
end
--获取字符串中唯一的字符个数
function get_distinct_num( num )
	local dist = {}
	local len = string.len( num )
	local out = ""
	local out1 = {}
	for i=1,len do
		c = string.sub(num,i,i)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	out1[1] = out

	return table.getn( dist ), out1
end

--=================胆拖计算是否中奖======================
function ComputeDT(queryStr,djStr)
	local codeD = {}
	local codeT = {}
	local codeDJ = {}
	local len = 0
	local flagD = {0,0}
	xItem = Split(queryStr,"x")
	len = string.len(xItem[1])
	for i = 1,len do
		codeD[i] = string.sub(xItem[1],i,i)
	end
	for i = 1,string.len(xItem[2]) do
		codeT[i] = string.sub(xItem[2],i,i)
	end
	for i = 1,3 do
		codeDJ[i] = string.sub(djStr,i,i)
	end
	for i = 1,3 do
		in_flag = 0
		for j = 1,len do
			if codeDJ[i] == codeD[j] then
				in_flag = 1
			end
		end
		for j=1,len do
			if codeDJ[i] == codeD[j] and flagD[j] == 0 then
				flagD[j] = 1
				break
			end
		end
		if in_flag == 0 then
			flagT = 0
			for j = 1,string.len(xItem[2]) do
				if	codeDJ[i] == codeT[j] then
					flagT = 1
				end
			end
			if flagT ~= 1 then
				return 0
			end
		end
	end
	for i=1,len do
		if flagD[i] ~= 1 then
			return 0
		end
	end
	return 1
end

--返回注数,倍投数 , 投注串, 指令串
function random_3d( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )
	if itemList[1] == '' then return 0 end
	--单式机选
	if butType == '1' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or not butNum or butNum > 5 or butNum < 1 then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		for i=1,butNum do
			local nums = selectNumber(3,0,1,2,3,4,5,6,7,8,9)
			zlStr = zlStr..nums[1]..nums[2]..nums[3]
			if i<butNum then zlStr = zlStr.."**" end
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_3d('1', zlStr)

	--组3机选
	elseif butType == '2' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or not butNum or butNum > 5 or butNum < 1 then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		for i=1,butNum do
			local nums = sel_dist_num(2,0,1,2,3,4,5,6,7,8,9)
			local num3 = selectNumber(1,nums[1],nums[2])
			zlStr = zlStr..nums[1]..nums[2]..num3[1]
			if i<butNum then zlStr = zlStr.."**" end
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_3d('2', zlStr)

	--组6机选
	elseif butType == '3' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or not butNum or butNum > 5 or butNum < 1 then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		for i=1,butNum do
			local nums = sel_dist_num(3,0,1,2,3,4,5,6,7,8,9)
			zlStr = zlStr..nums[1]..nums[2]..nums[3]
			if i<butNum then zlStr = zlStr.."**" end
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_3d('3', zlStr)
	end
	return butNum, butPower, butStr, zlStr

end

--返回注数,倍投数,投注串
function check_3d( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )
	if itemList[1] == '' then return 0 end

	qjStr = string.gsub( inStr, "#", "0")
	qxStr = string.gsub( qjStr, "*", "")

	if itemLen > 2 or itemLen < 1  or  string.find(qxStr,"%D") then return 0 end
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式直选	1#0301#注数#倍数
	if  butType =='1' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,butNum do
			if string.len(xxItem[i]) ~= 3 then
				return 0
			end
			butStr = butStr.."00"..format_num(butPower)..format_3d(xxItem[i]).."^"
		end

	--组3单式	1#0303#注数#倍数
	elseif  butType == '2' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem
		if butNum > 5 or butNum < 1  then
			return 0
		end

		for i=1,butNum do
			if string.len(xxItem[i]) ~= 3  then
				return 0
			end
			--判断分组情况
			temp = get_distinct_num( xxItem[i] )
			if temp ~= 2 then
				return 0
			else
				butStr = butStr.."01"..format_num(butPower)..sort_str(xxItem[i]).."^"
			end
		end

	--组6单式	1#0306#注数#倍数
	elseif  butType == '3' then
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem
		if butNum > 5 or butNum < 1  then
			return 0
		end

		for i=1,butNum do
			if string.len(xxItem[i]) ~= 3  then
				return 0
			end
			--判断分组情况
			temp = get_distinct_num( xxItem[i] )
			if temp ~= 3 then
				return 0
			else
				butStr = butStr.."02"..format_num(butPower)..sort_str(xxItem[i]).."^"
			end
		end


	--单选包点 1#0321#和数值#倍数
	elseif  butType == '4' then
		if tonumber(itemList[1]) > 27 then return 0 end

		butNum = D3BetNumPoint10[tonumber(itemList[1])+1]
		butStr = butStr.."10"..format_num(butPower)..format_num(itemList[1]).."^"


	--组3包点 1#0323#和数值#倍数
	elseif  butType == '5' then
		if tonumber(itemList[1]) > 26 or  tonumber(itemList[1]) < 1 then return 0 end

		butNum = D3BetNumPoint11[tonumber(itemList[1])]
		butStr = butStr.."11"..format_num(butPower)..format_num(itemList[1]).."^"


	--组6包点 1#0326#和数值#倍数
	elseif  butType == '6' then

		if tonumber(itemList[1]) > 24 or  tonumber(itemList[1]) < 3 then return 0 end

		butNum = D3BetNumPoint12[tonumber(itemList[1])]
		butStr = butStr.."12"..format_num(butPower)..format_num(itemList[1]).."^"


	--组合包点 1#0329#和数值#倍数
	elseif  butType == '7' then
		if tonumber(itemList[1]) > 26 or  tonumber(itemList[1]) < 1 then return 0 end

		butNum = D3BetNumPoint13[tonumber(itemList[1])+1]
		butStr = butStr.."13"..format_num(butPower)..format_num(itemList[1]).."^"

	--按位包号 1#0330#百位*十位*个位*倍数（每位均可选择1—10个号码）
	elseif  butType == '8' then

		xItem = Split(itemList[1], "*")
		if #xItem ~= 3  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) < 0 or string.len(xItem[i]) ~= get_distinct_num(xItem[i]) then
				return 0
			end

		end
		butNum = string.len(xItem[1]) * string.len(xItem[2]) * string.len(xItem[3])
		butStr = "20"..format_num( butPower )..format_num( string.len(xItem[1]) )..sort_str( xItem[1] ).."^"..format_num( string.len(xItem[2]) )..sort_str( xItem[2] ).."^"..format_num( string.len(xItem[3]) )..sort_str( xItem[3] ).."^"


	--单选复式 1#0341#自选号码#倍数（可选3—10个号码）
	elseif  butType == '9' then
		if string.len( itemList[1] ) < 3 or  string.len( itemList[1] ) > 10 or string.len(itemList[1]) ~= get_distinct_num(itemList[1]) then return 0 end

		butNum = D3BetDuplex0[string.len( itemList[1] )]
		butStr = "30"..format_num(butPower)..format_num(string.len(itemList[1]))..sort_str(itemList[1]).."^"


	--组3复试 1#0343#自选号码#倍数（可选2—10个号码）
	elseif  butType == 'a' then
		if string.len( itemList[1] ) < 2 or  string.len( itemList[1] ) > 10 or string.len(itemList[1]) ~= get_distinct_num(itemList[1]) then return 0 end

		butNum = D3BetDuplex1[string.len( itemList[1] )]
		butStr = "31"..format_num(butPower)..format_num(string.len(itemList[1]))..sort_str(itemList[1]).."^"

	--组6复试 1#0346#自选号码#倍数（可选4—10个号码）
	elseif  butType == 'b' then
		if string.len( itemList[1] ) < 4 or  string.len( itemList[1] ) > 10 or string.len(itemList[1]) ~= get_distinct_num(itemList[1]) then return 0 end

		butNum = D3BetDuplex2[string.len( itemList[1] )]
		butStr = "32"..format_num(butPower)..format_num(string.len(itemList[1]))..sort_str(itemList[1]).."^"


	--组合复式 1#0349#自选号码#倍数（可选2—10个号码）
	elseif  butType == 'c' then
		if string.len( itemList[1] ) < 2 or  string.len( itemList[1] ) > 10 or string.len(itemList[1]) ~= get_distinct_num(itemList[1]) then return 0 end

		butNum = D3BetDuplex3[string.len( itemList[1] )]
		butStr = "33"..format_num(butPower)..format_num(string.len(itemList[1]))..sort_str(itemList[1]).."^"


	--单选包串 1#0344#自选号码#倍数（可选2—10个号码）
	elseif  butType == 'd' then
		if string.len( itemList[1] ) < 2 or  string.len( itemList[1] ) > 10 or string.len(itemList[1]) ~= get_distinct_num(itemList[1]) then return 0 end
		butNum = D3BetDuplex4[string.len( itemList[1] )]
		butStr = "34"..format_num(butPower)..format_num(string.len(itemList[1]))..sort_str(itemList[1]).."^"


	--单选二重号 1#0345#自选号码#倍数（可选2—10个号码）
	elseif  butType == 'e' then
		if string.len( itemList[1] ) < 2 or  string.len( itemList[1] ) > 10 or string.len(itemList[1]) ~= get_distinct_num(itemList[1]) then return 0 end
		butNum = D3BetDuplex5[string.len( itemList[1] )]
		butStr = "35"..format_num(butPower)..format_num(string.len(itemList[1]))..sort_str(itemList[1]).."^"

	--单选全胆拖 1#0350#M*N#倍数（M个数字作为胆码，0<M<=2 N个不包括胆码中的数字作为拖码，N+M>=3且N>=2）
	elseif  butType == 'f' then
		xxStr = string.gsub(itemList[1], "*", "")
		xxItem = Split(itemList[1], "*")

		if string.len(xxItem[1]) > 2 or string.len(xxItem[1]) < 1 or string.len(xxItem[2]) < 2 or get_distinct_num(xxStr) > 10
		or string.len(xxItem[2]) ~= get_distinct_num(xxItem[2])
		or get_distinct_num(xxStr) ~= (get_distinct_num(xxItem[1]) + get_distinct_num(xxItem[2])) then return 0 end

		zs={{7,19,37,61,91,127,169,217,271},{12,18,24,30,36,42,48,54},{4,7,10,13,16,19,22,25,28}}
		if string.len(xxItem[1]) == 1 then butNum = zs[1][string.len(xxItem[2])]
		elseif  string.len(xxItem[1]) == get_distinct_num(xxItem[1]) then	butNum = zs[2][string.len(xxItem[2])]
		elseif  string.len(xxItem[1]) ~= get_distinct_num(xxItem[1]) then	butNum = zs[3][string.len(xxItem[2])]
		else return 0 end
		butStr = "50"..format_num(butPower)..sort_str(xxItem[1]).."*"..sort_str(xxItem[2]).."^"

	--组3胆拖 1#0351#M*N#倍数（M个数字作为胆码，0<M<=2；N个不包括胆码中的数字作为拖码，N+M>=3且N>=2；M=2时，胆码必须相同）
	elseif  butType == 'g' then
		xxStr = string.gsub(itemList[1], "*", "")
		xxItem = Split(itemList[1], "*")

		if string.len(xxItem[1]) > 2 or string.len(xxItem[1]) < 1 or string.len(xxItem[2]) < 1 or get_distinct_num(xxStr) > 10
		or string.len(xxItem[2]) ~= get_distinct_num(xxItem[2])
		or get_distinct_num(xxStr) ~= (get_distinct_num(xxItem[1]) + get_distinct_num(xxItem[2]))
		or (string.len(xxItem[1]) == 2 and get_distinct_num(xxItem[1]) ~=1 ) then return 0 end

		zs={{2,4,6,8,10,12,14,16,18},{1,2,3,4,5,6,7,8,9}}
		if string.len(xxItem[1]) == 1 then butNum = zs[1][string.len(xxItem[2])]
		elseif  string.len(xxItem[1]) == 2 then	butNum = zs[2][string.len(xxItem[2])]
		else return 0 end
		butStr = "51"..format_num(butPower)..sort_str(xxItem[1]).."*"..sort_str(xxItem[2]).."^"

	--组6胆拖 	1#0352#M*N#倍数（M个数字作为胆码，0<M<=2；N个不包括胆码中的数字作为拖码，N+M>=3且N>=2；M=2时，胆码必须不同）
	elseif  butType == 'h' then
		xxStr = string.gsub(itemList[1], "*", "")
		xxItem = Split(itemList[1], "*")

		if string.len(xxItem[1]) > 2 or string.len(xxItem[1]) < 1 or string.len(xxItem[2]) < 2 or get_distinct_num(xxStr) > 10
		 or string.len(xxItem[2]) ~= get_distinct_num(xxItem[2])
		or get_distinct_num(xxStr) ~= (get_distinct_num(xxItem[1]) + get_distinct_num(xxItem[2]))
		or (string.len(xxItem[1]) == 2 and get_distinct_num(xxItem[1]) ==1 ) then return 0 end

		zs={{nil,1,3,6,10,15,21,28,36},{1,2,3,4,5,6,7,8,nil}}
		if string.len(xxItem[1]) == 1 then butNum = zs[1][string.len(xxItem[2])]
		elseif  string.len(xxItem[1]) == 2 then	butNum = zs[2][string.len(xxItem[2])]
		else return 0 end
		butStr = "52"..format_num(butPower)..sort_str(xxItem[1]).."*"..sort_str(xxItem[2]).."^"

	--组合胆拖 	1#0353#M*N#倍数（M个数字作为胆码，0<M<=2；N个不包括胆码中的数字作为拖码，N+M>=3且N>=2）
	elseif  butType == 'i' then
		xxStr = string.gsub(itemList[1], "*", "")
		xxItem = Split(itemList[1], "*")

		if string.len(xxItem[1]) > 2 or string.len(xxItem[1]) < 1 or string.len(xxItem[2]) < 2 or get_distinct_num(xxStr) > 10
		or string.len(xxItem[2]) ~= get_distinct_num(xxItem[2])
		or get_distinct_num(xxStr) ~= (get_distinct_num(xxItem[1]) + get_distinct_num(xxItem[2])) then return 0 end

		zs={{3,6,10,15,21,28,36,45,55},{13,4,5,6,7,8,9,10,nil},{2,3,4,5,6,7,8,9,10}}
		if string.len(xxItem[1]) == 1 then butNum = zs[1][string.len(xxItem[2])]
		elseif  string.len(xxItem[1]) == get_distinct_num(xxItem[1]) then	butNum = zs[2][string.len(xxItem[2])]
		elseif  string.len(xxItem[1]) ~= get_distinct_num(xxItem[1]) then	butNum = zs[3][string.len(xxItem[2])]
		else return 0 end

		butStr = "53"..format_num(butPower)..sort_str(xxItem[1]).."*"..sort_str(xxItem[2]).."^"

	--单选单胆拖 	1#0354#M*N#倍数（M个数字作为胆码，0<M<=2；N个不包括胆码中的数字作为拖码，N+M>=3且N>=2；M=2时，胆码必须不同）
	elseif  butType == 'j' then
		xxStr = string.gsub(itemList[1], "*", "")
		xxItem = Split(itemList[1], "*")

		if string.len(xxItem[1]) > 2 or string.len(xxItem[1]) < 1 or string.len(xxItem[2]) < 2 or get_distinct_num(xxStr) > 10
		or string.len(xxItem[2]) ~= get_distinct_num(xxItem[2])
		or get_distinct_num(xxStr) ~= (get_distinct_num(xxItem[1]) + get_distinct_num(xxItem[2]))
		or (string.len(xxItem[1]) == 2 and get_distinct_num(xxItem[1]) ==1 ) then return 0 end

		zs={{nil,6,18,36,60,90,126,168,216},{6,12,18,24,30,36,42,48,nil}}
		if string.len(xxItem[1]) == 1 then butNum = zs[1][string.len(xxItem[2])]
		elseif  string.len(xxItem[1]) == 2 then	butNum = zs[2][string.len(xxItem[2])]
		else return 0 end
		butStr = "54"..format_num(butPower)..sort_str(xxItem[1]).."*"..sort_str(xxItem[2]).."^"

	--单选双胆拖 	1#0355#M*N#倍数（M个数字作为胆码，0<M<=2；N个不包括胆码中的数字作为拖码，N+M>=3且N>=2；M=2时，胆码必须相同）
	elseif  butType == 'k' then
		xxStr = string.gsub(itemList[1], "*", "")
		xxItem = Split(itemList[1], "*")

		if string.len(xxItem[1]) > 2 or string.len(xxItem[1]) < 1 or string.len(xxItem[2]) < 1 or get_distinct_num(xxStr) > 10
		or string.len(xxItem[2]) ~= get_distinct_num(xxItem[2])
		or get_distinct_num(xxStr) ~= (get_distinct_num(xxItem[1]) + get_distinct_num(xxItem[2]))
		or (string.len(xxItem[1]) == 2 and get_distinct_num(xxItem[1]) ~=1 ) then return 0 end

		zs={{6,12,18,24,30,36,42,48,54},{3,6,9,12,15,18,21,24,27}}
		if string.len(xxItem[1]) == 1 then butNum = zs[1][string.len(xxItem[2])]
		elseif  string.len(xxItem[1]) == 2 then	butNum = zs[2][string.len(xxItem[2])]
		else return 0 end
		butStr = "55"..format_num(butPower)..sort_str(xxItem[1]).."*"..sort_str(xxItem[2]).."^"

	--单选全跨度	1#0360#跨度#倍数（0—9跨度）
	elseif  butType == 'l' then

		if tonumber(itemList[1]) > 9 or string.len(itemList[1]) ~= 1  then return 0 end

		zs={10,54,96,126,144,150,144,126,96,54}
		butNum = zs[tonumber(itemList[1])+1]
		butStr = "60"..format_num(butPower)..format_num(itemList[1]).."^"

	--跨度组三 	1#0363#跨度#倍数（0—9跨度）
	elseif  butType == 'm' then

		if tonumber(itemList[1]) > 9 or tonumber(itemList[1]) < 1 or string.len(itemList[1]) ~= 1  then return 0 end

		zs={18,16,14,12,10,8,6,4,2}
		butNum = zs[tonumber(itemList[1])]
		butStr = "61"..format_num(butPower)..format_num(itemList[1]).."^"

	--跨度组六 	1#0366#跨度#倍数（0—9跨度）
	elseif  butType == 'n' then

		if tonumber(itemList[1]) > 9 or tonumber(itemList[1]) < 2 or string.len(itemList[1]) ~= 1  then return 0 end

		zs={8,14,18,20,20,18,14,8}
		butNum = zs[tonumber(itemList[1])-1]
		butStr = "62"..format_num(butPower)..format_num(itemList[1]).."^"

	--跨度组合 	1#0369#跨度#倍数（0—9跨度）
	elseif  butType == 'o' then

		if tonumber(itemList[1]) > 9 or tonumber(itemList[1]) < 1 or string.len(itemList[1]) ~= 1  then return 0 end

		zs={18,24,28,30,30,28,24,18,10}
		butNum = zs[tonumber(itemList[1])]
		butStr = "63"..format_num(butPower)..format_num(itemList[1]).."^"

	--和数尾组合 	1#0379#和尾数#倍数（将投注号码三位数相加之和的尾数称为“和数尾”。）
	elseif  butType == 'p' then

		if tonumber(itemList[1]) > 9 or string.len(itemList[1]) ~= 1  then return 0 end
		butNum = 22
		butStr = "73"..format_num(butPower)..format_num(itemList[1]).."^"

	--单选奇偶 	1#0380#注码奇偶序列#倍数（0代表偶数，1代表奇数）
	elseif  butType == 'q' then

		if string.len((string.gsub(itemList[1], "[01]", ""))) ~= 0 or string.len(itemList[1]) ~= 3  then return 0 end

		butNum = 125
		butStr = "80"..format_num(butPower)..format_3d(itemList[1]).."^"

	--组三奇偶 	1#0383#注码奇偶序列#倍数（0代表偶数，1代表奇数）
	elseif  butType == 'r' then

		if string.len((string.gsub(itemList[1], "[01]", ""))) ~= 0 or string.len(itemList[1]) ~= 3  then return 0 end

		zs = {["000"] = 20,["111"] = 20,["001"] = 25,["010"] = 25,["100"] = 25,["011"] = 25,["101"] = 25,["110"] = 25}

		if zs[itemList[1]] ~= nil then butNum = zs[itemList[1]] else return 0 end
		butStr = "81"..format_num(butPower)..sort_str(itemList[1]).."^"

	--组六奇偶 	1#0386#注码奇偶序列#倍数（0代表偶数，1代表奇数）
	elseif  butType == 's' then

		if string.len((string.gsub(itemList[1], "[01]", ""))) ~= 0 or string.len(itemList[1]) ~= 3  then return 0 end

		zs = {["000"] = 10,["111"] = 10,["001"] = 50,["010"] = 50,["100"] = 50,["011"] = 50,["101"] = 50,["110"] = 50}

		if zs[itemList[1]] ~= nil then butNum = zs[itemList[1]] else return 0 end
		butStr = "82"..format_num(butPower)..sort_str(itemList[1]).."^"

	--组合奇偶 	1#0389#注码奇偶序列#倍数（0代表偶数，1代表奇数）
	elseif  butType == 't' then

		if string.len((string.gsub(itemList[1], "[01]", ""))) ~= 0 or string.len(itemList[1]) ~= 3  then return 0 end

		zs = {["000"] = 35,["111"] = 35,["001"] = 75,["010"] = 75,["100"] = 75,["011"] = 75,["101"] = 75,["110"] = 75}

		if zs[itemList[1]] ~= nil then butNum = zs[itemList[1]] else return 0 end
		butStr = "83"..format_num(butPower)..sort_str(itemList[1]).."^"

	--单选大小 	1#0390#注码大小序列#倍数（0代表小数，1代表大数）
	elseif  butType == 'u' then
		if string.len((string.gsub(itemList[1], "[01]", ""))) ~= 0 or string.len(itemList[1]) ~= 3  then return 0 end

		butNum = 125
		butStr = "90"..format_num(butPower)..format_3d(itemList[1]).."^"

	--组三大小 	1#0393#注码大小序列#倍数（0代表小数，1代表大数）
	elseif  butType == 'v' then
		if string.len((string.gsub(itemList[1], "[01]", ""))) ~= 0 or string.len(itemList[1]) ~= 3  then return 0 end

		zs = {["000"] = 20,["111"] = 20,["001"] = 25,["010"] = 25,["100"] = 25,["011"] = 25,["101"] = 25,["110"] = 25}

		if zs[itemList[1]] ~= nil then butNum = zs[itemList[1]] else return 0 end
		butStr = "91"..format_num(butPower)..sort_str(itemList[1]).."^"

	--组六大小 	1#0396#注码大小序列#倍数（0代表小数，1代表大数）
	elseif  butType == 'w' then

		if string.len((string.gsub(itemList[1], "[01]", ""))) ~= 0 or string.len(itemList[1]) ~= 3  then return 0 end

		zs = {["000"] = 10,["111"] = 10,["001"] = 50,["010"] = 50,["100"] = 50,["011"] = 50,["101"] = 50,["110"] = 50}

		if zs[itemList[1]] ~= nil then butNum = zs[itemList[1]] else return 0 end
		butStr = "92"..format_num(butPower)..sort_str(itemList[1]).."^"

	--组合大小 	1#0399#注码大小序列#倍数（0代表小数，1代表大数）
	elseif  butType == 'x' then

		if string.len((string.gsub(itemList[1], "[01]", ""))) ~= 0 or string.len(itemList[1]) ~= 3  then return 0 end

		zs = {["000"] = 35,["111"] = 35,["001"] = 75,["010"] = 75,["100"] = 75,["011"] = 75,["101"] = 75,["110"] = 75}

		if zs[itemList[1]] ~= nil then butNum = zs[itemList[1]] else return 0 end
		butStr = "93"..format_num(butPower)..sort_str(itemList[1]).."^"

	else
		return 0
	end
	return butNum,butPower,butStr,inStr
end

function query_3d(butType,queryStr,djStr)
	local butPower = 1;
	local itemList = {}
	local result = {0,0,0}
	local itemLen = 0;
	local dj = ""
	local hezhi = 0
	local distNumQ = 0
	local distNumD
	local qStr = {}
	local dStr = {}
	local tempStr = {}
	local bingoNum = {0,0}
	local bingo = 0
	itemList = Split( queryStr, "#" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	xxStr = string.gsub(itemList[1], "*", "x")
	xxItem = Split(xxStr, "xx")
	--单式自选
	if butType == '1' then
		result[1] = MultSimpCompute(xxItem,djStr,butPower,3,1,butType)
	--组三自选
	elseif butType == '2' then
		djStr = sort_str_dj(djStr)
		for i =1 ,#xxItem do
			xxItem[i] = sort_str_dj(xxItem[i])
			if string.find(xxItem[i],djStr) then
				result[2] = result[2] + 1
			end
		end
		result[2] = result[2] * butPower
	--组六自选
	elseif butType == '3' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 3  then
			for i = 1,table.getn(xxItem) do
				distNumQ,qStr[1] = get_distinct_num(xxItem[i])
				if distNumQ == 3 then
					butType = '3'
					result[3] = result[3] + MultSimpCompute(qStr[1],djStr,butPower,3,1,butType)
				end
			end
		end
	--单选包点
	elseif butType == '4' then
		hezhi = tonumber(string.sub(djStr,1,1)) + tonumber(string.sub(djStr,2,2)) + tonumber(string.sub(djStr,3,3))
		if tonumber(itemList[1]) == hezhi then
			result[1] = butPower
		end
	--组三包点
	elseif butType == '5' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 2  then
			hezhi = tonumber(string.sub(djStr,1,1)) + tonumber(string.sub(djStr,2,2)) + tonumber(string.sub(djStr,3,3))
			if tonumber(itemList[1]) == hezhi then
				result[2] = butPower
			end
		end
	--组六包点
	elseif butType == '6' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 3  then
			hezhi = tonumber(string.sub(djStr,1,1)) + tonumber(string.sub(djStr,2,2)) + tonumber(string.sub(djStr,3,3))
			if tonumber(itemList[1]) == hezhi then
				result[3] = butPower
			end
		end
	--z组合包点
	elseif butType == '7' then
		distNumD = get_distinct_num(djStr)
		hezhi = tonumber(string.sub(djStr,1,1)) + tonumber(string.sub(djStr,2,2)) + tonumber(string.sub(djStr,3,3))
		if tonumber(itemList[1]) == hezhi then
			result[distNumD] = butPower
		end
	--按位包号
	elseif butType == '8' then
		xItem = Split(xxStr,"x")
		butType = '2'
		result[1] = MultSimpCompute(xItem,djStr,butPower,3,1,butType)
	--单选复式
	elseif butType == '9' then
		butType = '3'
		result[1] = MultSimpCompute(xxItem,djStr,butPower,3,1,butType)
	--组三复式
	elseif butType == 'a' then
		butType = '3'
		distNumD = get_distinct_num(djStr)
		if distNumD == 2 then
			result[2] = MultSimpCompute(xxItem,djStr,butPower,3,1,butType)
		end
	--组六复式
	elseif butType == 'b' then
		butType = '3'
		distNumD = get_distinct_num(djStr)
		if distNumD == 3 then
			result[3] = MultSimpCompute(xxItem,djStr,butPower,3,1,butType)
		end
	--组合复式
	elseif butType == 'c' then
		butType = '3'
		distNumD = get_distinct_num(djStr)
		result[distNumD] = MultSimpCompute(xxItem,djStr,butPower,3,1,butType)
	--单选包串
	elseif butType == 'd' then
		butType = '3'
		distNumD = get_distinct_num(djStr)
		if distNumD == 3 then
			result[1] = MultSimpCompute(xxItem,djStr,butPower,3,1,butType)
		end
	--单选二重号
	elseif butType == 'e' then
		butType = '3'
		distNumD = get_distinct_num(djStr)
		if distNumD == 2 then
			result[1] = MultSimpCompute(xxItem,djStr,butPower,3,1,butType)
		end
	----单选全胆拖
	elseif butType == 'f' then
		result[1] = ComputeDT(xxStr,djStr) * butPower
	----组3胆拖
	elseif butType == 'g' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 2 then
			result[2] = ComputeDT(xxStr,djStr) * butPower
		end
	--组6胆拖
	elseif butType == 'h' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 3 then
			result[3] = ComputeDT(xxStr,djStr) * butPower
		end
	--组合胆拖
	elseif butType == 'i' then
		distNumD = get_distinct_num(djStr)
		result[distNumD] = ComputeDT(xxStr,djStr) * butPower
	--单选单胆拖
	elseif butType == 'j' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 3 then
			result[1] = ComputeDT(xxStr,djStr) * butPower
		end
	--单选双胆拖
	elseif butType == 'k' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 2 then
			result[1] = ComputeDT(xxStr,djStr) * butPower
		end
	----单选全跨度
	elseif butType == 'l' then
		djStr = sort_str_dj(djStr)
		kuadu = tonumber(string.sub(djStr,3,3)) - tonumber(string.sub(djStr,1,1))
		if kuadu == tonumber(itemList[1]) then
			result[1] = butPower
		end
	--跨度组三
	elseif butType == 'm' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 2 then
			djStr = sort_str_dj(djStr)
			kuadu = tonumber(string.sub(djStr,3,3)) - tonumber(string.sub(djStr,1,1))
			if kuadu == tonumber(itemList[1]) then
				result[2] = butPower
			end
		end
	--跨度组六
	elseif butType == 'n' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 3 then
			djStr = sort_str_dj(djStr)
			kuadu = tonumber(string.sub(djStr,3,3)) - tonumber(string.sub(djStr,1,1))
			if kuadu == tonumber(itemList[1]) then
				result[3] = butPower
			end
		end
	--组合跨度
	elseif butType == 'o' then
		distNumD = get_distinct_num(djStr)
		if distNumD ~= 1 then
			djStr = sort_str_dj(djStr)
			kuadu = tonumber(string.sub(djStr,3,3)) - tonumber(string.sub(djStr,1,1))
			if kuadu == tonumber(itemList[1]) then
				result[distNumD] = butPower
			end
		end
	--和数尾组合
	elseif butType == 'p' then
		distNumD = get_distinct_num(djStr)
		hezhi = tonumber(string.sub(djStr,1,1)) + tonumber(string.sub(djStr,2,2)) + tonumber(string.sub(djStr,3,3))
		if hezhi % 10 == tonumber(itemList[1]) then
			result[distNumD] = butPower
		end
	--单选奇偶
	elseif butType == 'q' then
		for i=1,3 do
			qStr[i] = tonumber(string.sub(itemList[1],i,i))
			dStr[i] = tonumber(string.sub(djStr,i,i) % 2)
			if qStr[i] == dStr[i] then
				bingo = bingo + 1
			end
		end
		if bingo == 3 then
			result[1] = butPower
		end
	----组三奇偶
	elseif butType == 'r' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 2 then
			qStr[1] = sort_str_dj(itemList[1])
			djStr = justjiou(djStr)
			if string.find(djStr,qStr[1]) then
				result[2] = butPower
			end
		end
	--组六奇偶
	elseif butType == 's' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 3 then
			qStr[1] = sort_str_dj(itemList[1])
			djStr = justjiou(djStr)
			if string.find(djStr,qStr[1]) then
				result[3] = butPower
			end
		end
	--组合奇偶
	elseif butType == 't' then
		distNumD = get_distinct_num(djStr)
		qStr[1] = sort_str_dj(itemList[1])
		djStr = justjiou(djStr)
		if string.find(djStr,qStr[1]) then
			result[distNumD] = butPower
		end
	--单选全大小
	elseif butType == 'u' then
		for i=1,3 do
			qStr[i] = tonumber(string.sub(itemList[1],i,i))
			if tonumber(string.sub(djStr,i,i)) > 4 then
				dStr[i] = 1
			else
				dStr[i] = 0
			end
			if qStr[i] == dStr[i] then
				bingo = bingo + 1
			end
		end
		if bingo == 3 then
			result[1] = butPower
		end
	--组三大小
	elseif butType == 'v' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 2 then
			qStr[1] = sort_str_dj(itemList[1])
			djStr = justdaxiao(djStr)
			if string.find(djStr,qStr[1]) then
				result[2] = butPower
			end
		end
	--组六大小
	elseif butType == 'w' then
		distNumD = get_distinct_num(djStr)
		if distNumD == 3 then
			qStr[1] = sort_str_dj(itemList[1])
			djStr = justdaxiao(djStr)
			if string.find(djStr,qStr[1]) then
				result[3] = butPower
			end
		end
	--组合大小
	elseif butType == 'x' then
		distNumD = get_distinct_num(djStr)
		qStr[1] = sort_str_dj(itemList[1])
		djStr = justdaxiao(djStr)
		if string.find(djStr,qStr[1]) then
			result[distNumD] = butPower
		end
	end

	return result[1]..","..result[2]..","..result[3]
end

--test
--print(check_3d('1',"123**122**123#11"))
--print(check_3d('2',"123#122#723#778"))
--print(check_3d('3',"271#10"))
--print(check_3d('4',"8#10"))
--print(check_3d('6',"#99"))
--print(check_3d('9',"123#99"))
--print(check_3d('c',"13#99"))
--print((check_3d('c',"13#1")))
--local t1,t2,t3,t4,t5=pcall(check_3d,'6',"10#1")
--print(t1 or t2,t3,t4,t5)

--print(check_3d('6',"123789#12"))
--print(check_3d('7',"12369578#12"))
--print(check_3d('8',"02*01*08#1"))

--for i=1,10000 do
--	print(random_3d('1',"5"))
--	print(random_3d('2',"5"))
--	print(random_3d('3',"5#12"))
--end
--print(query_3d('1',"212**123**668#5","123"))
--print(query_3d('3',"212**132**321","123"))
--print(query_3d('4',"6#5","222"))
--print(query_3d('5',"7#5","033"))
--print(query_3d('8',"6*6*8#5","668"))
--print(query_3d('9',"123#5","333"))
--print(query_3d('b',"1263#5","123"))
--print(query_3d('c',"123#5","321"))
--print(query_3d('e',"123#5","323"))
--print(query_3d('f',"1*234#5","231"))
--print(query_3d('g',"1*423#5","122"))
--print(query_3d('h',"12*43#5","123"))
--print(query_3d('i',"1*43#5","114"))
--print(query_3d('j',"1*43#5","114"))
--print(query_3d('k',"1*43#5","114"))
--print(query_3d('r',"011#5","323"))
--print(query_3d('t',"111#5","153"))
--print(query_3d('v',"011#5","155"))
--print(query_3d('x',"111#5","555"))

